import { StringUtils } from './StringUtils'
import { UniStore } from './UniStorege'

export class LStore {
  static authReady(bool = 0) {
    sessionStorage.setItem("0x63", bool)
  }

  static admin(bool = false) {
    sessionStorage.setItem("0x69", bool)
  }

  static isA() {
    return !!sessionStorage.getItem("0x69")
  }

  static isAuthReady() {
    const value = +sessionStorage.getItem("0x63")
    return value && !isNaN(value)
  }
  static setAuthPath(json) {
    sessionStorage.setItem("0x41B8C1", JSON.stringify(json))
  }
  static getAuthPath() {
    return StringUtils.parseJson(sessionStorage.getItem("0x41B8C1"), [])
  }

  static setUserInfo(UserInfo, loginForm) {
    const { F_RealName, F_UserId, F_Account, F_HeadIcon, F_Role = [], F_Mobile,bc_id,bc_name } = UserInfo
    const roles = F_Role.map(item => item["F_EnCode"])
    console.log("用户角色", F_Role, roles)
    const { username, password } = loginForm || {}
    const info = {
      roleCode: roles,
      name: F_RealName,
      id: F_UserId,
      account: F_Account,
      iconSrc: F_HeadIcon,
      mobile: F_Mobile,
      username,
      password,
      bc_id,
      bc_name
    }

    UniStore.setItem("u_201", JSON.stringify(info))
  }

  static getUserInfo() {
    const loginInfoStr = UniStore.getItem("u_201")
    return StringUtils.parseJson(loginInfoStr, {})
  }

  static setBaseInfo(info) {
    UniStore.setItem("baseInfo_web", JSON.stringify(info))
  }

  static getBaseInfo(info) {
    const str = UniStore.getItem("baseInfo_web")
    return StringUtils.parseJson(str, {})
  }

  static getPadBaseInfo() {
    const storeBaseInfo = UniStore.getItem("BaseInfo")
    const baseInfo = StringUtils.parseJson(storeBaseInfo, {})

    return baseInfo
  }

  static setFuncMenus(menus = []) {
    UniStore.setItem("m_x32", JSON.stringify(menus || []))
  }

  static getFuncMenus() {
    const str = UniStore.getItem("m_x32")
    return StringUtils.parseJson(str, [])
  }

  static removeStore() {
    UniStore.removeItem("BaseInfo")
    UniStore.removeItem("UserInfo")
    UniStore.removeItem("currentUrl")
    UniStore.removeItem("tabProduction")
    UniStore.removeItem("u_201")
    UniStore.removeItem("m_x32")
    UniStore.removeItem("baseInfo_web")
  }
}
